/*
 *      Fischer Chess Club
 *      Copyright (c) 2010 Ryan Chiu
 *      All rights reserved.
 *      
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fischer;

import java.util.Arrays;

import fischer.commands.Tell;
import fischer.game.Moves;

public class Command {
	public static final String[] commands = { "+", "-", "=admin", "chessmove",
			"finger", "library", "tell" }; // Array of all server commands.

	public static String execute(String user, String command, String parameters)
			throws Exception {
		if (command.equals("chessmove")) {
			String algebraic = parameters.trim(); // Move in algebraic notation.
			Moves.makeMove(algebraic);
			return "";
		} else if (command.equals("tell")) {
			return Tell.process(parameters, user);
		} else if (command.equals("finger")) {
			return "Finger command";
		} else if (command.equals("library")) {
			return "Library command: using parameters \"" + parameters + "\"";
		} else if (command.equals("=admin")) {
			String result = "";
			for (int i = 1; i < List.loadAdminList().length; i++) {
				result += List.loadAdminList()[i] + " ";
			}
			return "Admin list: " + List.loadAdminList().length
					+ " name(s) - \n" + result;
		} else {
			return "Other...";
		}
	}

	public static String parse(String command, String user) throws Exception {
		String cmd_array[] = command.split(" ");
		String cmd = cmd_array[0];
		if (Arrays.asList(commands).contains(cmd)) {
			return execute(user, cmd, command.replaceAll(cmd, ""));
		} else {
			return cmd + ": Command not found.";
		}
	}
}
